$(function(){
		cart_list();
		$("body").delegate(".amount-plus","click",function(event){
			var input_num=$(this).parent('.amount-btn').siblings('input');
			var num=input_num.val();
			var goods_number=input_num.attr('data-goods-number');
			$.loading.start();
			$.post('/cart/edit_cart', {goods_number:goods_number,num:num,type:1,
			}, function(result) {
				if(result.code==1){
					cart_list();
				}else{
					$.loading.stop();
					layer.msg(result.message)
				}
			}, "json");
		})
		$("body").delegate(".amount-minus","click",function(event){
			var input_num=$(this).parent('.amount-btn').siblings('input');
			var num=input_num.val();
          	if(num==1){
              layer.msg('已经最少了')
              return;
            }
			var goods_number=input_num.attr('data-goods-number');
			$.loading.start();
			$.post('/cart/edit_cart', {goods_number:goods_number,num:num,type:2,
			}, function(result) {
				if(result.code==1){
					cart_list();
				}else{
					$.loading.stop();
					layer.msg(result.message)
				}
			}, "json");
		});
		// 商品选择框
		$("body").on('click', '.goods-checkbox', function() {
			var shop_id = $(this).data("shop-id");
			if ($(this).hasClass('select')) {
				$(this).removeClass('select');
				$(this).find(":checkbox").prop("checked", false);
				$('#cart_shop_' + shop_id).find(".shop-checkbox").removeClass('select');// 店铺全选取消
				$('.all-checkbox').removeClass('select');// 全选取消
			} else {
				$(this).addClass('select');
				$(this).find(":checkbox").prop("checked", true);
	
				if ($("#cart_shop_" + shop_id).find(":checkbox").size() == $("#cart_shop_" + shop_id).find(":checkbox:checked").size()) {
					$("#cart_shop_" + shop_id).find(".shop-checkbox").addClass('select');// 店铺全选选中
				}
	
				if ($("#cart_list").find(":checkbox").size() == $("#cart_list").find(":checkbox:checked").size()) {
					$('.all-checkbox').addClass('select');// 全选选中
				}
			}
			select();
		})

		// 店铺选择框
		$("body").on('click', '.shop-checkbox', function() {
			var shop_id = $(this).data("shop-id");
			if ($(this).hasClass('select')) {
				$('#cart_shop_' + shop_id).find('.cart-checkbox').removeClass('select');
				$('#cart_shop_' + shop_id).find(":checkbox").prop("checked", false);
				$('.all-checkbox').removeClass('select');// 全选取消
			} else {
				$('#cart_shop_' + shop_id).find('.cart-checkbox').addClass('select');
				$('#cart_shop_' + shop_id).find(":checkbox").prop("checked", true);
				if ($("#cart_list").find(":checkbox").length == $("#cart_list").find(":checkbox:checked").length) {
					
					$('.all-checkbox').addClass('select');// 全选选中
				}
			}
			select();
		})

		// 全选框
		$("body").on('click', '.all-checkbox', function() {
			if ($(this).hasClass('select')) {
				$('.cart-checkbox').removeClass('select');
				$("#cart_list").find(":checkbox").prop("checked", false);
			} else {
				$('.cart-checkbox').addClass('select');
				$("#cart_list").find(":checkbox").prop("checked", true);
			}
			select();
		})
	
		// 删除
		$("body").on('click', '.del', function() {
			var cart_ids = $(this).attr('data-cart-id');
			if (!cart_ids) {
				cart_ids = [];
				$("#cart_list").find(":checkbox:checked").each(function() {
					cart_ids.push($(this).val());
				})
			} 
			$.confirm("您确定要从购物车中移除选中的商品吗?", function() {
				$.loading.start();
				$.post('/cart/del_cart', {
					data_id: cart_ids,
				}, function(result) {
					if(result.code==1){
						cart_list();
					}
				}, "json");
			});
		});
		
		// 结算
		$("body").on('click', '.submit-btn', function() {
			$.post('/cart/pay_sub', {}, function(result) {
				if(result.code==1){
					self.location='/pay/paymentOrder'; 
				}else{
					self.location='/user/login'; 
				}
			}, "json");
	});
		
})

// 选择商品事件
function select() {
	cart_ids = [];
	$("#cart_list").find(":checkbox:checked").each(function() {
		cart_ids.push($(this).val());
	})	
	
	$.loading.start();
	
	$.post('/cart/count_cart', {
		data_id: cart_ids,
	}, function(result) {
		cart_list();
	}, "json");
}
	
function cart_list(){
	
	$.post('/cart/cart_list', {data:1,}, function(result) {
				
				if(result.code==1){
					
					shop_info=result.data;
					$('.count_num').html(result.message.num);
					$('.SZY-CART-SELECT-GOODS-NUMBER').html(result.message.num);
					$('.price1').html('￥'+result.message.price);
					var content='';
					$.each(shop_info, function(key, val) {
						var q_select=1;
						$.each(val, function(k, v) {
							if(v.select==0){
								q_select=0;
							}
						});
						
						if(q_select==1){
							var q_select=' select';
						}
						content+='<!-- 各个店铺下的信息 --><div id="cart_shop_'+key+'" class="order-body"><div class="shop"><div class="shop-info"><div class="cart-checkbox shop-checkbox '+q_select+'" data-shop-id="'+key+'" ><label for="">勾选此店铺下所有商品</label></div>&nbsp;&nbsp;<span class="shop-name">店铺：</span><a href="/store/index/id/'+key+'" target="_blank" title="'+val[0].store_name+'" class="shop-info-name">'+val[0].store_name+'</a></div></div><div class="order-content"><div class="item-list"><div class="bundle bundle-last">';
						$.each(val, function(k, v) {
							if(v.select==1){
								var qu_select='select';
								var qu_select2='checked="checked"';
							}else{
								var qu_select='';
								var qu_select2='';
							}
							content+='<!-- 购物车中商品列表 --><div class="item-holder" ><div class="item-body last-item"><ul class="item-content clearfix "><li class="td td-chk"><div class="td-inner"><div class="cart-checkbox goods-checkbox '+qu_select+'" shop-id="'+v.store_id+'" data-shop-id="'+v.shop_id+'"><input type="checkbox" name="checkbox" value="'+v.id+'" '+qu_select2+'><label for="">勾选商品</label></div></div></li><li class="td td-item"><div class="td-inner"><div class="item-pic"><a href="'+v.url+'" target="_blank" class=""><img src="'+v.shop_picture+'" class="itempic"></a></div><div class="item-info"><div class="item-basic-info"><a href="'+v.url+'" target="_blank" title="'+v.shop_name+'" class="item-title">'+v.shop_name+'</a></div></div></div></li><li class="td td-info"><div class="item-props">';
						 	if(v.sku_spec){
						 		content+='<p class="sku-line">规格：'+v.sku_spec+'</p>';
						 	}
						 	if(v.sku_style){
						 		content+='<p class="sku-line">颜色：'+v.sku_style+'</p>';
						 	}
						 	if(v.daiyan_sale>0){
								var sale_price='<em style="color: #f00;font-size: 12px;line-height: 17px;"><br/>券￥ '+v.dikouquan+'</em>';
						 	}else{
						 		var sale_price='';
						 	}
						 	var sj_price=v.num*v.price-v.num*v.daiyan_sale;
						 	content+='</div></li><li class="td td-price"><div class="td-inner"><div class="item-price"><div class="price-content"><div class="price-line"><em class="price-original">￥'+v.oprice+'</em></div><div class="price-line"><em class="price-now">￥'+v.price+'</em>'+sale_price+'</div></div></div></div></li><li class="td td-amount"><div class="td-inner"><div class="amount-wrapper"><div class="item-amount"><span class="amount-widget amount"><input type="text" class="amount-input" value="'+v.num+'" data-goods-number="'+v.id+'"maxlength="8" title="请输入购买量" disabled><span class="amount-btn"><span class="amount-plus"><i>+</i></span><span class="amount-minus"><i>-</i></span></span></span></div><div class="amount-msg" style="display:none"></div></div></div></li><li class="td td-sum"><div class="td-inner"><em class="number second-color">￥'+sj_price+'</em></div></li><li class="td td-op"><div class="td-inner"><a href="javascript:void(0);" class="del" data-cart-id="'+v.id+'">删除</a></div></li></ul></div></div>'
						})
						 
						content+='</div></div></div></div>';
						
					});
					$('#cart_list').html(content);
				}
				
				if(result.code==0){
					
					$('#content .content').html('');
					var content='<div class="cart-empty"><div class="message"><ul><li class="txt">购物车还是空空的呢，快去看看心仪的商品吧~</li><li><!-- 如果是未登录，显示登录按钮 _start--><!-- <a href="/" class="login-btn">登录</a> --><!-- 如果是未登录，显示登录按钮 _end--><a href="/" class="btn-link" title="去购物">去购物&gt;</a></li></ul></div></div>';
					$('#content .content').html(content);
				}
				$.loading.stop();
	}, "json");
}



